import {defineStore} from 'pinia';
//超声视频阅片页store
const useVideo = defineStore('videoData', {
  state: (): any => ({
    activeColor: "#70E2F0",//标注工具的激活颜色
    loading: true,
    currentSeriesUid: '',//当前series uid
    activeAnnoTab: 'multi',//多帧标注、单帧标注tab，multi or single
    singleAnnoList: [],//所有序列的单帧标注列表
    activeFrame: null,//选中的帧段信息
    currnetToolName: '',//当前工具
  }),
  getters: {
    isMulti():any{//帧段标注
      return this.activeAnnoTab === 'multi'
    },
    isSingle():any{//单帧标注
      return this.activeAnnoTab === 'single'
    },
    currentSingleList():any{//当前序列单帧列表
      return this.getSingleAnnoListBySuid(this.currentSeriesUid)
    },
  },
  actions: {
    setLoading(isLoading: boolean) {
      this.loading = isLoading;
    },
    getSingleList(totalList: any, groupList: any) {//从接口数据中筛选单帧数据，不在group中的结节属于单帧
      const groupIds = groupList.map((item: any) => item.uuid)
      return totalList.filter((it: any) => !groupIds.includes(it.resultGroupUuid))
    },
    getActiveSingleItem(){//当前选中的单帧
      return this.currentSingleList.find((item: any) => item.isActive)
    },
    setSingleSelected(sUid: any, id: any){//高亮某个单帧列表中
      const selected = this.singleAnnoList.find((item: any) => item.id === sUid)
      selected && selected.list.map((it: any) => {
        (it.id === id) ? (it.isActive = !it.isActive) : (it.isActive = false)
      });
    },
    unSelectedAllSingle(){//失活当前序列的单帧列表的所有项
      this.currentSingleList.map((it: any) => it.isActive = false)
    },
    toggleSingleHide(id: any){//显示、隐藏单帧项
      this.singleAnnoList.map((item: any) => {
        const selected = item.list.find((it: any) => it.id === id);
        selected && (selected.hide = !selected.hide)
      })
    },
    hideAllSingle(index: number, hide: boolean){//显示、隐藏单帧列表中所有项
      this.singleAnnoList[index].list.map((item: any) => {
        item.hide = hide
      })
    },
    delSingle(id: any){//删除某个单帧
      this.singleAnnoList.map((item: any) => {
        const selectedIndex = item.list.findIndex((it: any) => it.id === id);
        (selectedIndex > -1) && item.list.splice(selectedIndex, 1)
      })
    },
    addSingle(sUid: any,item: any){//某个序列单帧列表新增一个单帧项
      const currentSeries = this.singleAnnoList.find((item: any) => item.id === sUid)
      currentSeries && currentSeries.list.push(item)
    },
    getSingleAnnoListBySuid(sUid: any){//某个序列的单帧列表
      const singleObj = this.singleAnnoList.find((item: any) => item.id === sUid)
      return singleObj && singleObj.list || []
    },
    getSingleListItemById(id: any){//获取某个id的单帧数据
      for(let item of this.singleAnnoList){
        const singleData = item.list.find((it: any) => it.id === id)
        if(singleData){
          return singleData
        }
      }
      return undefined
    },
    getSingleItemByUuid(uuid: any){//获取某个uuid的单帧数据
      for(let item of this.singleAnnoList){
        const singleData = item.list.find((it: any) => it.data.uuid === uuid)
        if(singleData){
          return singleData
        }
      }
      return undefined
    },
    getSingleSeriesItemById(id: any){//获取某个单帧id对应的序列单帧数据
      return this.singleAnnoList.find((item: any) => {
        return item.list.find((it: any) => it.id === id) != undefined
      })
    },
    getSingleSeriesIndexById(id: any){//获取某个单帧id对应的序列序号
      return this.singleAnnoList.findIndex((item: any) => {
        return item.list.find((it: any) => it.id === id) != undefined
      })
    },
  }
});

export default useVideo;